global data_folder_final "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"

use "W:\intimate\data\match_victim_data_clean_new", clear

gen dv_sample=1
append using "W:\intimate\data\match_MFvictim_data_clean_new"
replace dv_sample=0 if dv_sample==. 

g perp_other_sample = 0
append using "W:\intimate\data\match_suspectalt_data_clean_new"
replace perp_other_sample = 1 if missing(perp_other_sample)
drop pre_crimes_dummy

// create relevant variables
gen move_pre=0
replace move_pre=1 if kuntaB5 != kuntaB1
replace move_pre = . if missing(kuntaB5)
replace move_pre = . if missing(kuntaB1)

gen fin_lang = (kieli == "fi")
gen sp_fin_lang =  (spouse_kieli == "fi")

gen fertility=nchildB1>0

forvalues s = 1/4 {
	g cohabit`s' = 1 - missing(sphnroB`s')
}
g spouse_missing = 0
replace spouse_missing = 1 if missing(spouse_pre_age)
	
drop depression
merge m:1 shnro year_event using "W:\intimate\data\prescriptions_depression_indlevel.dta"
drop if _merge==2 
drop _merge 
replace depression=0 if depression==.
replace ndepression=0 if ndepression==. 

gen spouse_pre_crimes_dummy=(spouse_pre_crimes>0)

gen gap_emp=spouse_pre_ptoim1-ptoim1B1
gen gap_earnings=spouse_pre_tyotu-tyotuB1


local table_varlist age college high_school ho fin_lang fertility nchildB1 ptoim1B1  tyotuB1 pre_crimes year_start_cohab age_yst ndistinct avg_relation yr_since_grad depression ndepression move_pre pre_crime spouse_missing spouse_pre_age cohabit1 cohabit2 cohabit3 cohabit4 spouse_college spouse_high_school spouse_ho sp_fin_lang spouse_pre_ptoim1 spouse_pre_tyotu spouse_pre_crimes_dummy spouse_pre_crimes gap_emp gap_earnings

eststo victim: estpost sum `table_varlist' if dv_couple==1 & dv_sample==1
eststo match1: estpost sum  `table_varlist' if dv_couple==0 & dv_sample==1

eststo mf: estpost sum `table_varlist' if dv_couple==1 & dv_sample==0
eststo match2: estpost sum `table_varlist' if dv_couple==0 & dv_sample==0

eststo perp_other: estpost sum `table_varlist' if suspect_alt==1 & perp_other_sample ==1
eststo match3: estpost sum `table_varlist' if suspect_alt==0 & perp_other_sample ==1


esttab victim match1 perp_other match3 mf match2 using "$result_folder\Table_balance_victim_mf_match_full.tex", replace cell("mean (pattern (1 1 1 1) fmt(2))") modelwidth (10) mtitle("Victims" "Match Control" "Other Perp Partners" "Match Control" "MF Crime Partners" "Match Control")  nonumber  coeflabel (age "Age"college "Share college" high_school "Share high school" ho "Share dropouts" fin_lang "Finnish" fertility "Fertility" nchildB1 "No. of Children" ptoim1B1 "Employment" tyotuB1 "Earnings"pre_crimes "Prior Crimes" year_start_cohab "year start cohabitation"  cum_record "Previous crimes" age_yst "Age of youngest child" ndistinct "No. of previous relationships" avg_relation "Avg. relationship years" yr_since_grad "Years since graduation" depression "Share depression" ndepression "Number of depression" move_pre "Move across municipality" spouse_missing "Spouse missing" spouse_pre_age "Spousal age" cohabit1 "Cohabitation t=-1" cohabit2 "Cohabitation t=-2" cohabit3 "Cohabitation t=-3" cohabit4 "Cohabitation t=-4" spouse_college "Spouse share college" spouse_high_school "Spouse share high school"  spouse_ho "Spouse share dropouts" sp_fin_lang "Spouse Finnish" spouse_pre_ptoim1 "Spouse employment" spouse_pre_tyotu "Spouse earnings" spouse_pre_crimes_dummy "Share of spouses having prior crimes" spouse_pre_crimes "Spouse no. of prior crimes" gap_emp "Employment Gap (Men-Women)" gap_earnings "Earnings Gap (Men-Women)") title (Balance Table) nonotes addn (Data from Flee)

